package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevTimingTaskDao;
import com.sugon.entity.DevTimingTaskEntity;
import com.sugon.service.DevTimingTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工单定时任务Service实现类
 *
 * @author sugon
 * @date 2019-08-08 08:32:02
 */
@Service("devTimingTaskService")
public class DevTimingTaskServiceImpl implements DevTimingTaskService {
    @Autowired
    private DevTimingTaskDao devTimingTaskDao;

    /**
     * 查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevTimingTaskEntity queryObject(String id) {
        return devTimingTaskDao.queryObject(id);
    }

    /**
     * 条件查询
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevTimingTaskEntity> queryList(Map<String, Object> map) {
        return devTimingTaskDao.queryList(map);
    }

    /**
     * 条件查询数目
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devTimingTaskDao.queryTotal(map);
    }

    /**
     * 保存
     *
     * @param devTimingTask 实体
     * @return
     */
    @Override
    public int save(DevTimingTaskEntity devTimingTask) {
        devTimingTask.setId(RandomUtil.randomString(32));
        return devTimingTaskDao.save(devTimingTask);
    }

    /**
     * 更新
     *
     * @param devTimingTask 实体
     * @return
     */
    @Override
    public int update(DevTimingTaskEntity devTimingTask) {
        return devTimingTaskDao.update(devTimingTask);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devTimingTaskDao.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devTimingTaskDao.deleteBatch(ids);
    }

    /**
     * 保存人物
     *
     * @param id
     * @param type
     * @return
     */
    @Override
    public int saveTask(String id, Integer type) {
        DevTimingTaskEntity devTimingTask = new DevTimingTaskEntity();
        Date nowTime = new Date();
        devTimingTask.setId(RandomUtil.randomString(32));
        devTimingTask.setDelFlag(1);
        devTimingTask.setProcessId(id);
        devTimingTask.setStartTime(nowTime);
        devTimingTask.setTimingType(type);//1为30分钟  2为120分钟
        long currentTime;
        //判断当前状态
        if (1 == type) {
            currentTime = System.currentTimeMillis() + 30 * 60 * 1000;
        } else {
            currentTime = System.currentTimeMillis() + 120 * 60 * 1000;
        }
        Date date = new Date(currentTime);
        devTimingTask.setEndTime(date);//到时时间
        return devTimingTaskDao.save(devTimingTask);
    }

    /**
     * @return
     */
    @Override
    public List<DevTimingTaskEntity> getOutTime() {
        return devTimingTaskDao.getOutTime();
    }

    /**
     * 更新状态
     *
     * @param entity
     * @return
     */
    @Override
    public int updateStatus(DevTimingTaskEntity entity) {
        return devTimingTaskDao.updateStatus(entity);
    }

    /**
     * 删除流程
     *
     * @param type
     * @param processId
     * @return
     */
    @Override
    public int deleteByProcessId(Integer type, String processId) {
        return devTimingTaskDao.deleteByProcessId(type, processId);
    }

    /**
     * @param processId
     * @return
     */
    @Override
    public int deleteByProcessIds(String processId) {
        return devTimingTaskDao.deleteByProcessIds(processId);
    }
}
